package samsung.yx;

import java.util.Scanner;

public class Biorhythms {
	public static void main(String[] args) {
		Scanner input = new Scanner(System.in);
		int p = 0, e = 0, i = 0, d = 0;
		int k = 0;
		while (k != -1) {
			p = input.nextInt();
			e = input.nextInt();
			i = input.nextInt();
			d = input.nextInt();
			if (p == -1 && e == -1 && i == -1 && d == -1) {
				break;
			}
			k++;
			int days = (5544 * p + 14421 * e + 1288 * i - d) % (21252);
			if (days <= 0) {
				days = 23 * 28 * 33 + days;
			}
			System.out.println("Case " + k
					+ ": the next triple peak occurs in " + days + " days.");
		}
	}

}
